package uk.ac.ebi.pride.tools.pap.db;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Florian Reisinger
 *         Date: 17-Feb-2009
 * @since 0.1
 */
public class LocalHsqldbConnectionFactory implements ConnectionProvider {

    private static final Logger log = Logger.getLogger(LocalHsqldbConnectionFactory.class);

    private static final LocalHsqldbConnectionFactory instance = new LocalHsqldbConnectionFactory();

    private LocalHsqldbConnectionFactory() {

    }

    public static LocalHsqldbConnectionFactory getInstance() {
        return instance;
    }

    public Connection getConnection() throws SQLException {
        Connection connection = null;
        try {
            // Load the JDBC driver
            String driverName = "org.hsqldb.jdbcDriver";
            Class.forName(driverName);

            // Create a connection to the database
            String url = "jdbc:hsqldb:hsql://localhost/xdb";
            String username = "sa";
            String password = "";
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("HSQL catalog: " + connection.getCatalog());
        } catch (ClassNotFoundException e) {
            log.error("Could not find the hsqldb database driver!");
            System.exit(-1);
        } catch (SQLException e) {
            log.error("Could not connect to the hsqldb database!");
            System.exit(-2);
        }
        return connection;
    }
}
